package com.sylli.oeuf.server.db.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import com.sylli.oeuf.server.exception.BasedException;
import com.sylli.oeuf.server.exception.ExceptionCause;
import com.sylli.oeuf.server.object.transferable.NPCTO;

public class MySQLNPC extends MySQLBasedProvider<NPCTO> {
	
	public MySQLNPC() {
		interactTableName = "npc";
	}

	@Override
	protected PreparedStatement convertObjectToInsertQuery(NPCTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"INSERT INTO " + interactTableName 
					+ " (name, flag)"
					+ " VALUES (?,?)", 
					Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, obj.getName());
			ps.setInt(2, obj.getFlag());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryCreationFail, e);
		}
	}

	@Override
	protected PreparedStatement convertObjectToUpdateQuery(NPCTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"UPDATE " + interactTableName 
					+ " SET name=?, flag=? WHERE npc_id = ?;");
			ps.setString(1, obj.getName());
			ps.setInt(2, obj.getFlag());
			ps.setInt(3, obj.getNpcId());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryUpdateFail, e);
		}
	}

	@Override
	protected NPCTO convertResultSetToObject(ResultSet rs)
			throws BasedException {
		try {
			NPCTO sto = new NPCTO();
			rs.first();
			sto.setNpcId(rs.getInt(1));
			sto.setName(rs.getString(2));
			sto.setFlag(rs.getInt(3));
			return sto;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		}
	}

	@Override
	protected Collection<NPCTO> convertResultSetToObjects(ResultSet rs)
			throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	protected PreparedStatement convertObjectToInsertQuery(
			Collection<NPCTO> objs) throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}


}
